<?php

class Attachment
{
    private $apiurl;
    private $apis;
    private $action;
    private $start = 0;
    private $length = 100;

    public function __construct($apiurl) {
        $this->apiurl   = $apiurl;
        $this->apis     = array(
            'getDiaryAttachments', 'delDiaryAttachment', 'getTopicAttachments', 'delTopicAttachment', 'getManualImage', 'updateManualImage'
        );
    }

    public function execute ($action) {
        if (empty($action)) $action = 'getTopicAttachments';
        if (!in_array($action, $this->apis)) return false;

        $this->action = $action;
        $result = $this->$action(request());
        return $result;
    }

    /**
     * 获取圈子话题附件列表
     * @param unknown $req
     * @return unknown
     */
    private function getTopicAttachments ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (empty($res) || empty($res['list'])) return false;

        $res['list'] = $this->showPreviewOfImage($res['list']);
        return render('attachment', $res);
    }

    /**
     * 删除圈子话题附件
     * @param unknown $req
     * @return unknown
     */
    private function delTopicAttachment ($req) {
        $param = $this->filterArgumentsOfPassed($req);

        global $_LOGGER;
        $_LOGGER->debug('delTopicAttachment', $param);

        $res = requestApi($param);
        return $res;
    }

    /**
     * 获取点滴附件列表
     * @param unknown $req
     * @return unknown
     */
    private function getDiaryAttachments ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (empty($res) || empty($res['list'])) return false;

        $res['list'] = $this->showPreviewOfImage($res['list']);
        return render('attachment', $res);
    }

    /**
     * 删除点滴附件
     * @param unknown $req
     * @return unknown
     */
    private function delDiaryAttachment ($req) {
        $param = $this->filterArgumentsOfPassed($req);

        global $_LOGGER;
        $_LOGGER->debug('delDiaryAttachment', $param);

        $res = requestApi($param);
        return $res;
    }

    private function showPreviewOfImage ($list) {
        $thumb_switch = true;
        $prefix = 'http://img.fenfenriji.com';
        $thumb = $thumb_switch ? '!thumb80' : '';

        foreach ($list as &$value) {
            $value['uploadTime'] = date('Y.m.d H:i', $value['uploadTime']);
            $value['attachmentPathOrigin'] = $prefix.$value['attachmentPath'];
            $value['attachmentPath'] = $prefix.$value['attachmentPath'].$thumb;
        }

        return $list;
    }

    private function showManualImage ($list) {
        $thumb_switch = true;
        $prefix = 'http://img.fenfenriji.com';
        $thumb = $thumb_switch ? '!thumb80' : '';

        foreach ($list as &$value) {
            $value['createTime'] = date('Y.m.d H:i', $value['create_time']);
            $value['urlOrigin'] = $prefix.$value['url'];
            $value['url'] = $prefix.$value['url'].$thumb;
            $value['rate'] = number_format($value['rate'], 2, '.', '');
            $value['rate'] = $value['rate'] * 100;
            $value['rate'] = $value['rate'] . '%';
            $value['optrate'] = '';
            $value['status'] == 1 && $value['optrate'] = '删除';
            $value['status'] == 2 && $value['optrate'] = '忽略';
            $value['textdes'] = '正常值';
            $value['label'] == 1 && $value['textdes'] = '色情值';
        }

        return $list;
    }

    /**
     * 获取需人工审核的列表
     * @param unknown $req
     * @return unknown
     */
    private function getManualImage ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        
        //if (empty($res) || empty($res['list'])) return false;

        $res['list'] = $this->showManualImage($res['list']);
        //$res['ymd'] = date('Y-m-d');
        return render('manualImage', $res);
    }

    /**
     * 人工审核的列表删除图片
     * @param unknown $req
     * @return unknown
     */
    private function updateManualImage ($req) {
        $param = $this->filterArgumentsOfPassed($req);

        /*global $_LOGGER;
        $_LOGGER->debug('delDiaryAttachment', $param);*/

        $res = requestApi($param);
        return $res;
    }

    /**
     * 传参过滤
     * @param unknown $req
     * @return multitype:string number NULL
     */
    private function filterArgumentsOfPassed($req) {
        switch ($this->action){
            case in_array($this->action, array('getTopicAttachments','getDiaryAttachments')):
                $param = array(
                    'do'     => 'attachment',
                    'format' => 'json',
                    'action' => $this->action,
                    'index'  => intval($req['index']) ? intval($req['index']) : 0,
                    'start'  => intval($req['page']) ? ((intval($req['page']) - 1) * $this->length) : $this->start,
                    'length' => intval($req['length']) ? intval($req['length']) : $this->length,
                );
                break;
            case in_array($this->action, array('delTopicAttachment','delDiaryAttachment')):
                $param = array(
                    'do'     => 'attachment',
                    'format' => 'json',
                    'action' => $this->action,
                    'index'  => intval($req['index']) ? intval($req['index']) : 0,
                    'id'     => intval($req['id']),
                    'uid'    => intval($req['uid']),
                    'tid'    => intval($req['bodyId']),
                    'bodyId' => intval($req['bodyId']),
                );
                break;
            case $this->action == "getManualImage":
                $param = array(
                    'do'     => 'attachment',
                    'format' => 'json',
                    'action' => $this->action,
                    'taskStatus' => intval($req['taskStatus']) ? intval($req['taskStatus']) : 0,
                    'type' => isset($req['type']) ? intval($req['type']) : 2,
                    'ymd' => !empty($req['ymd']) ? date('Ymd', strtotime($req['ymd'])) : '',
                    'start'  => intval($req['page']) ? ((intval($req['page']) - 1) * $this->length) : $this->start,
                    'length' => intval($req['length']) ? intval($req['length']) : $this->length,
                );
                break;
            case $this->action == "updateManualImage":
                $param = array(
                    'do'     => 'attachment',
                    'format' => 'json',
                    'action' => $this->action,
                    'id'     => intval($req['id']),
                    'uid'    => intval($req['uid']),
                    'mid'    => intval($req['mid']),
                    'bodyId' => intval($req['bodyId']),
                    'status' => intval($req['status']),
                    'tableName' => $req['tableName'],
                    'tableIndex' => $req['tableIndex'],
                );
                break;
        }

        return $param;
    }

}

$req = request();
$attachment   = new Attachment($_SC ['apiurl']);
echo $attachment->execute($req['op']);

